package com.itboye.hutouben.activity.mysetting;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.base.MyApplcation;
import com.itboye.hutouben.logincontroller.LoginController;
import com.itboye.hutouben.logincontroller.UnLoginState;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.SPUtils;
import com.umeng.message.UTrack;

import static com.itboye.hutouben.util.SPUtils.get;

/**
 * 设置
 */
public class SettingActivity extends BaseActivity {
    TextView version_up, version_now, about_tv, exit_tv;
    TextView add_shap_title_tv;
    ImageView close_icon;
    public static String EXITCHANGE = "com.itboye.exit";
    boolean isLogin = true;
    RelativeLayout update_pwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        add_shap_title_tv.setText("设置");
        boolean isLogin=(Boolean)SPUtils.get(this,null,Const.IS_LOGINED,false);
        if (isLogin) {
            update_pwd.setVisibility(View.VISIBLE);
            exit_tv.setVisibility(View.VISIBLE);
        }else {
            update_pwd.setVisibility(View.GONE);
            exit_tv.setVisibility(View.GONE);
        }
        isLogin = (boolean) get(this, null, Const.IS_LOGINED, false);
        if (isLogin) {
            exit_tv.setVisibility(View.VISIBLE);
        } else {
            exit_tv.setVisibility(View.GONE);
        }
        try {
            // 获取packagemanager的实例
            PackageManager packageManager = getPackageManager();
            // getPackageName()是你当前类的包名，0代表是获取版本信息
            PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0);
            String version = packInfo.versionName;

            version_now.setText(version);
        } catch (Exception e) {
        }


    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    private void deleteInfo() {
        LoginController.setLoginState(new UnLoginState());
        MyApplcation.getInstance().mPushAgent.removeAlias(
                get(this, null, Const.ID, "") + "", "htb",
                new UTrack.ICallBack() {

                    @Override
                    public void onMessage(boolean arg0, String arg1) {
                        // TODO Auto-generated method stub
                        System.out.println("---退出登录>" + arg0 + "---" + arg1);
                    }
                });
        SPUtils.put(this, null, Const.ID, "");
        SPUtils.put(this, null, Const.USERNAME, "");
        SPUtils.put(this, null, Const.PASSWORD, "");
        SPUtils.put(this, null, Const.MOBILE, "");
        SPUtils.put(this, null, Const.IS_LOGINED, false);
        SPUtils.put(this, null, Const.HEAD, "");
        SPUtils.put(this, null, Const.NICK, "");
        SPUtils.put(this, null, Const.YQM, "");
        SPUtils.put(this, null, Const.PaySecret, "");


    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
            case R.id.about_tv:
                intent = new Intent(SettingActivity.this, AboutActivity.class);
                startActivity(intent);
                break;
            case R.id.exit_tv:
//              intent = new Intent("exit");
//              sendBroadcast(intent);
                deleteInfo();
                Intent intent1 = new Intent(SettingActivity.EXITCHANGE);// 通知MeFragment更新界面
                sendBroadcast(intent1);
                finish();
                break;
            case R.id.update_pwd:
                intent = new Intent(this,ActivityJiDeUserPass.class);
                startActivity(intent);
                break;
        }
    }
}
